package database;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import object.District;
import utility.StringUtil;
import clientMain.Login;

public class DistrictDelegate {

	public boolean CheckInsertDisName(String name,int id){
		boolean rs = false;
		try {	
			  Statement st = Login.conn.createStatement();
			  ResultSet  traVe = st.executeQuery("select  * from District where DistrictName  =N'"+name+"'  and ProvinceId = "+id+"   ");
			  while (traVe.next()) { 
				  rs = true;
			  }
			  return rs;
			  
		} catch (Exception e) {
			
		}
		return rs;
	}
	public Vector<District> getListDistrict(int id, String sort, boolean flag){
		Vector<District> list =  new Vector<District>();
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_ListDistrict(?,?,?)}");
			cst.setInt(1, id);
			cst.setString(2, sort);
			cst.setBoolean(3, flag);
			ResultSet rs = cst.executeQuery();
			while(rs.next()){
				District dis = new District();
				dis.setDistrictId(rs.getInt("DistrictId"));
				dis.setDistrictName(StringUtil.getFilterIllegal(rs.getString("DistrictName")));
				list.add(dis);
			}
			Login.conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			list = null;
		}
		return list;
	}
	public boolean insertDistrict(District dis, int userInsert){
		boolean flag = true;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_InsertDistrict(?,?,?)}");
			cst.setString(1, StringUtil.setFilterIllegal(dis.getDistrictName()));
			cst.setInt(2, dis.getProvinceId());
			cst.setInt(3, userInsert);
			cst.executeUpdate();
			Login.conn.commit();
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}
	public boolean updateDistrict(District dis, int userUpdate){
		boolean flag = true;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_UpdateDistrict(?,?,?,?)}");
			cst.setInt(1, dis.getDistrictId());
			cst.setString(2, StringUtil.setFilterIllegal(dis.getDistrictName()));
			cst.setInt(3, dis.getProvinceId());
			cst.setInt(4, userUpdate);
			cst.executeUpdate();
			Login.conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
	public boolean CheckUpdateDisName(String name){
		boolean rs = false;
		try {	
			  Statement st = Login.conn.createStatement();
			  ResultSet  traVe = st.executeQuery("select * from District where DistrictName =N'"+name+"'");
			  while (traVe.next()) {
					  rs = true;
				  
			  }
			  return rs;
			  
		} catch (Exception e) {
			
		}
		return rs;
	}
	public District getDisName(int  disId){
		District dis = new District();
		try {	
			  Statement st = Login.conn.createStatement();
			  ResultSet  traVe = st.executeQuery("select DistrictName,ProvinceId from District where DistrictId ="+disId+"");
			  while (traVe.next()) {
				      
					  dis.setDistrictName(StringUtil.getFilterIllegal(traVe.getString("DistrictName")));
				      dis.setProvinceId(traVe.getInt("ProvinceId"));
			  }
			  return dis;
			  
		} catch (Exception e) {
			
		}
		return dis;
	
	}
	
}
